package com.paireach.bw.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.blankj.utilcode.utils.DeviceUtils;
import com.blankj.utilcode.utils.NetworkUtils;
import com.blankj.utilcode.utils.PhoneUtils;
import com.paireach.bw.R;
import com.paireach.bw.application.MyApplication;
import com.paireach.bw.base.BaseActivity;
import com.paireach.bw.bean.Driver;
import com.paireach.bw.common.Constant;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.UserRequest;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.utils.JsonParse;
import com.paireach.bw.utils.LogUtil;

import java.util.Set;
import java.util.UUID;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import es.dmoral.toasty.Toasty;

/**
 * Created by ${cqc} on 2016/11/4.
 */

public class LoginActivity extends BaseActivity {
    private static final String TAG = "LoginActivity";
    private EditText et1;
    private EditText et2;
    private Button btn;
    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            Transition explode = TransitionInflater.from(this).inflateTransition(android.R.transition.explode);
            explode.addListener(new Transition.TransitionListener() {
                @Override
                public void onTransitionStart(Transition transition) {

                }

                @Override
                public void onTransitionEnd(Transition transition) {
                    loginApp();
                }

                @Override
                public void onTransitionCancel(Transition transition) {

                }

                @Override
                public void onTransitionPause(Transition transition) {

                }

                @Override
                public void onTransitionResume(Transition transition) {

                }
            });
            getWindow().setEnterTransition(explode);
        }

        setContentView(R.layout.activity_login);
        context = this;
        findViews();
        initViews();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissProgressDialog();
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.USER_LOGIN) {
            dismissProgressDialog();
            JsonParse.parseLoginUser(event.getJson());
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.USER_LOGIN) {
            dismissProgressDialog();
            showError(event.getErrorMessage());
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if ("parseLoginUser".equals(event.getTag())) {
            if (event.getCode() == 1) {
                Driver driver = (Driver) event.getObj1();
                //存储手机号和密码
                String pwd = et2.getText().toString().trim();
                MyApplication.getSpUtils().putString(Constant.LOGIN_NAME, driver.getTel());
                MyApplication.getSpUtils().putString(Constant.TEL, driver.getTel().trim());//防止有空格，空格是鹰眼的非法字符
                MyApplication.getSpUtils().putString(Constant.PWD, pwd);
                MyApplication.getSpUtils().putString(Constant.REAL_NAME, driver.getName().trim());
                MyApplication.getSpUtils().putInt(Constant.ID, driver.getId());
                MyApplication.getSpUtils().putString(Constant.DRIVER_NUM, driver.getDriverNum());

                ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(LoginActivity.this);
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                ActivityCompat.startActivity(LoginActivity.this, intent, options.toBundle());
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        LoginActivity.this.finish();
                    }
                }, 500);
            } else {
                showError("" + event.getExtraInfo());
            }
        }
    }


    private void findViews() {
        et1 = (EditText) findViewById(R.id.et1_login_activity);
        et2 = (EditText) findViewById(R.id.et2_login_activity);
        btn = (Button) findViewById(R.id.btn_login_activity);
    }

    private void initViews() {
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginApp();
            }
        });

        et1.setText(MyApplication.getSpUtils().getString(Constant.LOGIN_NAME));
        et2.setText(MyApplication.getSpUtils().getString(Constant.PWD));
    }

    private void loginApp() {
        // DeviceUtils
        boolean deviceRoot = DeviceUtils.isDeviceRooted();//判断设备是否rooted
        int sdkVersion = DeviceUtils.getSDKVersion();//获取设备系统版本号
        String androidID = DeviceUtils.getAndroidID();//获取设备AndroidID
        String macAddress = DeviceUtils.getMacAddress();//获取设备MAC地址
        String manufacturer = DeviceUtils.getManufacturer();//获取设备厂商
        String model = DeviceUtils.getModel();//获取设备型号
        //NetworkUtils
        boolean is4G = NetworkUtils.is4G();//判断网络是否是4G
        boolean wifiEnabled = NetworkUtils.getWifiEnabled();//判断wifi是否打开
        String networkOperatorName = NetworkUtils.getNetworkOperatorName();//获取移动网络运营商名称
        //PhoneUtils
        String imei = PhoneUtils.getIMEI();//获取IMEI码
        String imsi = PhoneUtils.getIMSI();//获取IMSI码
        int phoneType = PhoneUtils.getPhoneType();//获取移动终端类型
        String simOperatorName = PhoneUtils.getSimOperatorName();//获取Sim卡运营商名称
        String simOperatorByMnc = PhoneUtils.getSimOperatorByMnc();
        String phoneStatus = PhoneUtils.getPhoneStatus();//获取手机状态信息
        TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String simSerialNumber = manager.getSimSerialNumber();//simCardNo
        String phone = et1.getText().toString().trim();
        String pwd = et2.getText().toString().trim();
//        LogUtil.d(TAG, "deviceRoot=" + deviceRoot + ",sdkVersion=" + sdkVersion + ",androidID=" + androidID
//                + ",macAddress=" + macAddress + ",manufacturer=" + manufacturer + ",model=" + model
//                + ",is4G=" + is4G + ",wifiEnabled=" + wifiEnabled + ",networkOperatorName=" + networkOperatorName
//                + ",simOperatorByMnc=" + simOperatorByMnc + ",imei=" + imei + ",imsi=" + imsi +
//                ",simSerialNumber=" + simSerialNumber+",phone="+phone);
        if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(pwd)) {
            showToast("用户名或密码不可为空");
        } else if (NetworkUtils.isConnected()) {
            showProgressDialog();
            UserRequest.passwordLogin_app(phone, pwd, deviceRoot, sdkVersion, androidID, macAddress, manufacturer,
                    model, is4G, wifiEnabled, networkOperatorName, imei, imsi, phoneType, simOperatorName, phoneStatus, simSerialNumber);
        } else {
            showToast("请检查您的网络连接再登录");
        }
    }
}
